from eventServer import eventServer
from itemBase import ItemBase
from vec3 import Vec3

class CarryCube(ItemBase):
    
    def __init__(self):
        ItemBase.__init__(self)
        
        self.setVar("mass", 2.0)
        self.setVar("base", "carryCube")
        self.setVar("scale", Vec3(0.5, 0.5, 0.5))
	self.setVar("texture", "potato")
        
        eventServer.register('itemDropped', self.onDrop)
        
    def onDrop(self, cmd, args):
        player = self.getVar("itemPlayer")
        if(not(player == None)):
            if(args['player'] == player.getVar('player')):
                itemPlayer = self.getVar("itemPlayer")
                eventServer.pull('onItemDrop',{'player':itemPlayer,"item":self.getVar("name")})
                self.setVar("itemPlayer", None)
                self.setVar("sides",['s','s','s','s'])
                v = self.getVar("vel")
                self.setVar("vel", v * 2)
            
    def update(self, cmd, args):
        ItemBase.update(self, cmd, args)
        
        player = self.getVar("itemPlayer")
        if(not(player == None)):
            
            player.setVar("itemCount", player.getVar("itemCount") + args["dtime"])
            
            v = self.getVar("vel")
            playV = player.getVar("vel")
            f = player.getVar("face")
            p = self.getVar("pos")
            playP = player.getVar("pos")

            v.x = playV.x
            v.y = playV.y
            if(f == "left"):
                p.x = playP.x - 1
                p.y = playP.y
 
            elif(f == "right"):
                p.x = playP.x + 1
                p.y = playP.y

    def itemCollision(self, player):
        if(player.getVar("carryFlag") and player.getVar("itemCount") < 0):
            eventServer.pull('onItemPickup',{'player':player.getVar("player"), 'item':self.getVar("name")})
            self.setVar("itemPlayer", player)
            self.setVar("sides",['t','t','t','t'])
            player.setVar("itemCount", 0.1)
